package com.uade.tp3.g06;

import android.app.ListActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import com.google.gson.Gson;

public class MatchViewer extends ListActivity {
	public static final String PREFS_FILE = "PrefsFile";
	public static final String SERVICE_ENCUENTROS = "http://ecancha.com.ar/ws.php?modulo=encuentro&action=query&nro=";
	public static final String SERVICE_LOGIN = "http://ecancha.com.ar/ws.php?modulo=login&action=logon&id=";

    /** Called when the activity is first created. */

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.matchviewer);
        
        SharedPreferences settings = getSharedPreferences(PREFS_FILE, 0);
        String sCelular = settings.getString("celular", "");
        
        JsonRetriever retriever = new JsonRetriever();
        String json = retriever.getJsonFromUrl(SERVICE_ENCUENTROS + sCelular);
        
        Gson gson = new Gson();
        MatchList listaPartidos = gson.fromJson(json, MatchList.class);
        
//        List<Match> listaPartidos = listaPartidos.listaPartidos;
//        Iterator <Match> iterator=null;
//        iterator = listaPartidos.iterator();
//        while(iterator.hasNext()) { }
        
//        Type listType = new TypeToken<Match>(){}.getType();
//        Match encuentros = new Gson().fromJson(json, listType);
        
//        Toast.makeText(MatchViewer.this, encuentros, Toast.LENGTH_LONG).show();
        
        
//        List<Match> mObjectList = new ArrayList<Match>();
//        MatchList list = null;
//        list = getMatchList(json);
        
//        List <MatchList> match_list = list.getMatchList();
//        MatchContainer mc = null;
//        for (int i = 0; i < list.size(); i++) {
//        	mc = match_list.get(i);
//        	mObjectList.add(mc.getMatch()); //Adding each match to the list
//        }
//        INICIO CODIGO LISTVIEW
//        setListAdapter(new ArrayAdapter<String>(MatchViewer.this, R.layout.list_item, mObjectList));
//        ListView lv = getListView();
//        lv.setTextFilterEnabled(true);
//        lv.setOnItemClickListener(new OnItemClickListener() {
//        	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//        		// When clicked, show a toast with the TextView text
////        		Toast.makeText(getApplicationContext(), ((TextView)view).getText(), Toast.LENGTH_SHORT).show();
//        		Toast.makeText(MatchViewer.this, "Partido Clickeado", Toast.LENGTH_LONG).show();
//        	}
//        });
//        FIN CODIGO LISTVIEW
	}
	
	protected MatchList getMatchList(String jsonString) {
		MatchList ml = null;
		Gson gson = new Gson();
		ml = gson.fromJson(jsonString, MatchList.class);
		return ml;
	}
}
